package com.xyebank.ledger.service.zy.adapter;

import org.apache.commons.lang3.StringUtils;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.springframework.stereotype.Component;

import com.xyebank.ledger.db.entitys.LApplicationInf;
import com.xyebank.ledger.service.common.conveter.ApplyBeanConverter;
import com.xyebank.ledger.service.constant.CommonConstant;
import com.xyebank.ledger.service.constant.ProductConstant;
import com.xyebank.ledger.util.DateConverter;
import com.xyebank.ledger.util.IDUtil;
import com.xyebank.ledger.util.StatusConvertUtil;
import com.xyebank.ledger.vo.deal.ZYCashApplicationInfVo;

@Component(value = "zyApplyBeanConverter")
public class ZYApplyBeanConverter implements ApplyBeanConverter<ZYCashApplicationInfVo> {

    ModelMapper modelMapper = new ModelMapper();

    // 不能采取注入方式
    private DateConverter dateConverter = new DateConverter();

    class ZYApplyPropertyMap extends PropertyMap<ZYCashApplicationInfVo, LApplicationInf> {
        protected void configure() { //注意　不要有重复的映射，不然会报错
            // TODO 解决自动映射id问题
            map(source.getOrderNo(), destination.getOrgOrderNo());
            map(source.getMblNo(), destination.getMblNo());
            using(dateConverter).map(source.getApplyTime(), destination.getApplyTime()); //　日期格式转换错误
            map(source.getApproveTime(), destination.getCreditTime());
            map(source.getApproveAmount(), destination.getCreditAmount());
       }
    }

    public ZYApplyBeanConverter() {
        modelMapper.addMappings(new ZYApplyPropertyMap());
    }

    @Override
    public LApplicationInf covert(ZYCashApplicationInfVo x) {
        LApplicationInf entity = modelMapper.map(x, LApplicationInf.class);
        buildDataSource(x, entity);
        
        entity.setId(IDUtil.generateId(x.getId().toString(), "zy"));
        entity.setXyOrgNo(ProductConstant.ZY_LOAN_ORG);
        entity.setXyProductNo(ProductConstant.ZY_LOAN_PROD_ID);
        if(x.getApproveStatus() != null)entity.setStatus(StatusConvertUtil.convertApplyStatus(x.getApproveStatus()));
        return entity;
    }

    private void buildDataSource(ZYCashApplicationInfVo x, LApplicationInf entity) {
        String loanNo = x.getLoanNo();
        String str = loanNo.substring(5, 6);
        if(StringUtils.equals(str, "4")){
            entity.setDataSource(CommonConstant.SJD_STR);
            
        } else if(StringUtils.equals(str, "3")){
            entity.setDataSource(CommonConstant.JRY_STR);
            
        } else{
            entity.setDataSource(CommonConstant.UNKOWN_STR);
            
        }
    }
    
    public static void main(String[] args){
        String str = "12345678";
        String a = str.substring(5,6);
        System.out.println(a);
        System.out.println(Integer.MAX_VALUE);
        
    }
}
